/*
 * 文件名：LocalRotaServiceImpl.java	 
 * 时     间：下午3:36:32
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.local.dao.mapers.LocalRotaUserMapper;
import com.local.model.LocalRotaUser;
import com.local.model.LocalRotaUserExample;
import com.local.page.LocalRotaUserPage;
import com.local.service.face.LocalRotaUserService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

import common.cz0735.coustomer.mapers.CoustomerInfoMapper;
import common.cz0735.coustomer.model.CoustomerInfo;
import common.cz0735.coustomer.model.CoustomerInfoExample;

/** 
 * 类描述:这里是类描述
 * @ClassName: LocalRotaServiceImpl 
 * @author Administrator
 * @date 2015年10月3日 下午3:36:32 
 */
@Service
public class LocalRotaUserServiceImpl extends
BaseServiceImpl<LocalRotaUserExample, LocalRotaUser, String> implements LocalRotaUserService
{
	@Autowired
	private LocalRotaUserMapper localRotaUserMapper;
	
	@Autowired
	private CoustomerInfoMapper coustomerInfoMapper;
	
	@Override
	public BaseMapper<LocalRotaUserExample, LocalRotaUser, String> getMapper()
	{
		// TODO Auto-generated method stub
		return localRotaUserMapper;
	}

	@Override
	public void updateByRotaUuid(String rotaUuid,String userIdString)
	{
		// TODO Auto-generated method stub
		try
		{
			String[] da =userIdString.split(",");
			localRotaUserMapper.deleteRotaUserId(rotaUuid);
			for(String userId:da){
				LocalRotaUser lr=new LocalRotaUser();
				lr.setUuid(UUIDGenerateUtil.generate());
				lr.setRotaUuid(rotaUuid);
				lr.setUserId(userId);
				localRotaUserMapper.insert(lr);
			}
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			throw new BaseException(e.getMessage());
		}
	}

	@Override
	public void blindUserToStation(LocalRotaUser localRotaUser) {
		try
		{
			localRotaUserMapper.blindUserToStation(localRotaUser.getUuid(),localRotaUser.getStationUuid());
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			throw new BaseException(e.getMessage());
		}
	}
	

	@Override
	public void blindUserAndStation(LocalRotaUser localRotaUser) {
		try
		{
			if(Tools.isEmpty(localRotaUser.getUuid())){
				localRotaUser.setUuid(UUIDGenerateUtil.generate());
				localRotaUserMapper.insert(localRotaUser);
			}else{
				localRotaUserMapper.updateByPrimaryKey(localRotaUser);
			}
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			throw new BaseException(e.getMessage());
		}
	}

	@Override
	public List<LocalRotaUser> roteUserToStationPage(LocalRotaUserPage<LocalRotaUser> pageInfo) {
		Integer recordCount = localRotaUserMapper.countRoteUserToStationPage(pageInfo);
		pageInfo.setTotalRecords(recordCount);
		List<LocalRotaUser> list = localRotaUserMapper.roteUserToStationPage(pageInfo);
		for (LocalRotaUser localRotaUser : list) {
			if(!Tools.isEmpty(localRotaUser.getUserId())){
				CoustomerInfo coustomerInfo = coustomerInfoMapper.selectByPrimaryKey(localRotaUser.getUserId());
				localRotaUser.setUserName(coustomerInfo.getName());
			}
		}
		pageInfo.setData(list);
		return list;
	}
	
	
	@Override
	public LocalRotaUser queryStationByUser(String userId)
	{
		// TODO Auto-generated method stub
		if(!Tools.isEmpty(userId)){
			
			CoustomerInfoExample condtion=new CoustomerInfoExample();
			condtion.or().andLoginUserUuidEqualTo(userId);
			List<CoustomerInfo> li=coustomerInfoMapper.selectByCondition(condtion);
			if(!Tools.isEmpty(li)&&li.size()>0){
				LocalRotaUser lr=localRotaUserMapper.queryStationByUser(li.get(0).getUuid());
				if(!Tools.isEmpty(lr)){
					return lr;
				}
			}
		}
		return null;
	}

	@Override
	public LocalRotaUser queryRotaUserStationByUuid(String uuid) {
		return localRotaUserMapper.selectByPrimaryKey(uuid);
	}

	@Override
	public void delRotaUserStationByUuid(String uuid) {
		try
		{
			localRotaUserMapper.deleteByPrimaryKey(uuid);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			throw new BaseException(e.getMessage());
		}	
	}
	
	
	
	
	
	
}
